package com.gzw.dms.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.adapter.EquipmentAdapter;
import com.gzw.dms.adapter.FPOrderAdapter;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.EquipmentInfo;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EquipmentActivity extends BaseActivity {
    private ListView lv_order;
    private Context mContext;
    private ProgressDialog mProgressDialog;
    private TextView tv_seleteAll;
    private EquipmentAdapter equipmentAdapter;
    private int refresh = 0;
    private List<EquipmentInfo> equipmentInfo;
    private boolean isCheck = false;
    private android.support.v4.widget.SwipeRefreshLayout mSwipeLayout;
    private View progress;
    private String page = "100";
    private String rows = "0";
    private String car_number = "";
    private String equipment_number = "";
    private String userName = "";
    private Intent mIntetn;
    private String driver_id = "";
    private String user_name = "";
    private List<String> ls_map;
    private TextView tv_select;
    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_equipment);

    }

    @Override
    protected void initViews() {
        lv_order = (ListView) findViewById(R.id.lv_order);
//        tv_seleteAll = (TextView) findViewById(R.id.ic_head1).findViewById(R.id.tv_selectAll);
        mSwipeLayout = (android.support.v4.widget.SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
        progress = findViewById(R.id.include_pb);
//        tv_select = (TextView) findViewById(R.id.tv_select);
        toolbar = (Toolbar) findViewById(R.id.toolbar);

    }

    @Override
    protected void initData() {
        mContext = this;
        ls_map = new ArrayList<>();
        mIntetn=getIntent();
        setSupportActionBar(toolbar);
        driver_id = (String)mIntetn.getExtras().get("driver_id");
        user_name = (String)mIntetn.getExtras().get("username");
        toolbar.setTitle("设备信息");
        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute();}

    @Override
    protected void initEvents() {
//        tv_seleteAll.setOnClickListener(new myOnClick());
        lv_order.setOnItemClickListener(new mOnItemClick());
//        tv_select.setOnClickListener(new myOnClick());
        toolbar.setNavigationOnClickListener(new myOnClick());

        mSwipeLayout.setOnRefreshListener(new android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh = 1;
                ShowOrderTask showOrderTask = new ShowOrderTask();
                showOrderTask.execute();
            }
        });
    }


    class mOnItemClick implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EquipmentInfo equipmentInfo = (EquipmentInfo) parent.getItemAtPosition(position);
            String equipment_number = equipmentInfo.getEquipment_number();
            String userName = equipmentInfo.getUsername();
            Intent mIntetn = new Intent();
            Bundle bundle = new Bundle();
            mIntetn.putExtra("fpstste", "0");
            mIntetn.putExtra("driver_id", driver_id);
            mIntetn.putExtra("equipment_number", equipment_number);
            mIntetn.putExtra("username", userName);
            mIntetn.setClass(mContext, Main2Activity.class);
            mIntetn.putExtras(bundle);
            startActivityForResult(mIntetn, 3);
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        if (resultCode == 1) {
//            refresh = 0;
//            ShowOrderTask showOrderTask = new ShowOrderTask();
//            showOrderTask.execute();
//        }else if(resultCode == 2){
//            refresh = 0;
//            String order_num1 = (String) data.getExtras().get("order_num");
//            String shiping_order_goid1 = (String) data.getExtras().get("shiping_order_goid");
//            String custom_name1
//                    = (String) data.getExtras().get("custom_name");
//            String receipt_name1
//                    = (String) data.getExtras().get("receipt_name");
//
//            ShowOrderTask showOrderTask = new ShowOrderTask();
//            showOrderTask.execute();
//        }

    }

    /**
     * Created by Wangzhiqiang on 2015-10-11
     * 单击事件
     */
    class myOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {

                default:
                    finish();
                    break;
            }

        }
    }


    // 刷新listview和TextView的显示
    private void dataChanged() {
        // 通知listView刷新
        equipmentAdapter.notifyDataSetChanged();
    }


    public class ShowOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            if (refresh == 0) {
//                mSwipeLayout.setRefreshing(true);
                mProgressDialog = ProgressDialog.show(mContext, null, "正在加载中...");
                mProgressDialog.setCancelable(true);
                mProgressDialog.setCanceledOnTouchOutside(false);

            } else if (refresh == 1) {
                mSwipeLayout.setRefreshing(true);
            }
        }

        protected String doInBackground(String... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            map.put("page", page);
            map.put("row",rows);
            map.put("userName",userName);
            map.put("car_number",car_number);
            map.put("equipment_number",equipment_number);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appGetEquipmentManList"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            if (refresh == 0) {
//                mSwipeLayout.setRefreshing(false);
                mProgressDialog.dismiss();

            } else if (refresh == 1) {
                mSwipeLayout.setRefreshing(false);
            }

            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {
                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else if (result.indexOf("successMsg") > 0){
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    equipmentInfo = JsonUtil.getPersons(successMsg, new TypeToken<List<EquipmentInfo>>() {
                    }.getType());
                    equipmentAdapter = new EquipmentAdapter(mContext, equipmentInfo,user_name);
                    lv_order.setAdapter(equipmentAdapter);

                }
            }
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu_home; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_vehicle, menu);
        getMenuInflater().inflate(R.menu.menu_sure, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_sure:
                EquipmentInfo equipmentInfo1 = new EquipmentInfo();
                for (int i = 0; i < equipmentInfo.size(); i++) {
                    if (equipmentAdapter.getIsSelected().get(i)) {
                        equipmentInfo1 = equipmentInfo.get(i);
                    }
                }
                if(equipmentInfo1.getEquipment_number()==null){
                    T.showShort(mContext,"请先选择要签收的运单!");
                }else {
                    String equipment_number1 = equipmentInfo1.getEquipment_number();
                    String userName = equipmentInfo1.getUsername();
                    Intent mIntetn = new Intent();
                    Bundle bundle = new Bundle();
                    mIntetn.putExtra("fpstste", "0");
                    mIntetn.putExtra("driver_id", driver_id);
                    mIntetn.putExtra("equipment_number", equipment_number1);
                    mIntetn.putExtra("username", userName);
                    mIntetn.setClass(mContext, Main2Activity.class);
                    mIntetn.putExtras(bundle);
                    startActivityForResult(mIntetn, 1);
                }
                break;
        }
        return super.onOptionsItemSelected(item);

    }


}
